﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Input;
using HtmlAgilityPack;
using SC2TV.Object;
using SC2TV.Tools;

namespace SC2TV.ViewModel
{
    public class PrivateMessageWriterViewModel : BaseViewModel
    {
        private string messageFor;

        public string MessageFor
        {
            get { return messageFor; }
            set { messageFor = value; OnPropertyChanged(); }
        }

        public string Header { get; set; }
        public string Message { get; set; }
        private string requestUrl;
        private string token;
        private string formID;

        public void Load(string requestUrl, string userName, string token, string formID)
        {
            this.requestUrl = requestUrl;
            MessageFor = userName;
            this.token = token;
            this.formID = formID;
        }

        public ICommand SendMessage
        {
            get
            {
                return new ActionCommand(async () =>
                {
                    var response = new Response();
                    try
                    {
                        var requester = new Requester(requestUrl);
                        response = await requester.Get();
                        var html = new HtmlDocument();
                        html.LoadHtml(response.Html);
                        token = token ?? html.DocumentNode.SelectSingleNode("//*[@id=\"edit-privatemsg-new-form-token\"]").GetAttributeValue("value", "");
                        var body = new Collection<KeyValuePair<string, string>>
                        {
                            new KeyValuePair<string, string>("recipient", MessageFor),
                            new KeyValuePair<string, string>("subject", Header),
                            new KeyValuePair<string, string>("body", Message),
                            new KeyValuePair<string, string>("form_token",token),
                            new KeyValuePair<string, string>("form_id", formID),
                            new KeyValuePair<string, string>("op", "Отправить сообщение")
                        };

                        response = await requester.Post(body);

                    }
                    catch (WebException e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    catch (Exception e)    //Exception происходит из-за метода EnsureSuccessStatusCode() в Requester.
                    {
                        Console.WriteLine(e.Message);
                        MessageBox.Show(response.StatusCode == HttpStatusCode.OK ||
                                        response.StatusCode == HttpStatusCode.Forbidden   //незнаю почему приходит 403, но сообщение отправляется.
                            ? "Сообщение отправлено"
                            : response.StatusCode.ToString());
                    }
                });
            }
        }
    }
}
